﻿using System.IO;
using FileSynchronizer.DataAccess;
using FileSynchronizer.Forms;
using mylibrary;

namespace FileSynchronizer.Objects
{
    //---------------------------------------------------------------------------------------------------------------------------------------
    // Code based upon the State Pattern, as described by the GOF
    //
    // http://en.wikipedia.org/wiki/State_pattern and http://en.wikipedia.org/wiki/Talk:State_pattern#Disputed
    //
    // This is a Concrete State Object
    //
    //---------------------------------------------------------------------------------------------------------------------------------------
    
    class ProgramState_Enterprise_SyncFolderPair : ProgramState
    {
         #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------
        public ProgramState_Enterprise_SyncFolderPair(ProgramContext TheProgramContext)
            : base(TheProgramContext)
        {
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Public Methods
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when we want to Synchronize all FolderPairs
        //---------------------------------------------------------------------------------------------------------------------------------------
        public override bool SynchronizeFolderPair(FolderPair TheFolderPair)
        {
            Form_Progress frm_SyncProgress;
            ProgramState_Idle NextState;

            bool LatencyOK = true;
            bool OperationResult = true;

            if (Directory.Exists(TheFolderPair.LeftFolder) & Directory.Exists(TheFolderPair.RightFolder))
            {
                if (CurrentProgramContext.Settings.LatencyEnabled)
                {
                    if (CurrentProgramContext.Settings.LatencyTimeout <= (NetworkDriveHelper.PathLatency(TheFolderPair.LeftFolder) + NetworkDriveHelper.PathLatency(TheFolderPair.RightFolder)))
                    {
                        LatencyOK = false;
                        // Log it?
                    }
                }
                if (LatencyOK)
                {
                    frm_SyncProgress = new Form_Progress(TheFolderPair);
                    frm_SyncProgress.ShowDialog();
                    if (!frm_SyncProgress.SyncSuccessfull)
                    {
                        Program.TheErrorForm.AddErrorString("Failed to synchronize " + TheFolderPair.LeftFolder + " with " + TheFolderPair.RightFolder);
                    }
                    else
                    {
                        OperationResult = false;
                    }
                    frm_SyncProgress = null;
                }

                NextState = new ProgramState_Idle(CurrentProgramContext);
                ChangeState(NextState);
                CurrentProgramContext.CurrentState = State.Idle;
            }
            return OperationResult;
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

   }
}
